<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
load()->func('tpl');
$shset=pdo_get('wff_system',array('uniacid'=>$_W['uniacid']));

$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
// if(isset($_GPC['keywords'])){
//     $where.="  and (title LIKE  concat('%', :name,'%') or phone LIKE  concat('%', :name,'%'))";
//     $data[':name']=$_GPC['keywords']; 
// }
if (!empty($_GPC['state'])) {
  $where=' WHERE  a.uniacid=:uniacid and a.state=:state  ';
  $data[':uniacid']=$_W['uniacid'];
  $data[':state']=$_GPC['state'];
  $sql="SELECT a.*,b.username,c.title FROM ".tablename('wff_assess') . " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc";
  $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('wff_assess') . " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc",$data);
  $active=$_GPC['state'];
}else{
  $where=' WHERE  a.uniacid=:uniacid ';
  $data[':uniacid']=$_W['uniacid'];
  $sql="SELECT a.*,b.username,c.title FROM ".tablename('wff_assess') . " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc";
  $total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('wff_assess') . " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id ".$where." ORDER BY a.time desc",$data);
}

$select_sql =$sql." LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$assesss=pdo_fetchall($select_sql,$data);
$pager = pagination($total, $pageindex, $pagesize);

// if (!empty($_GPC['state'])) {
// 	$active=$_GPC['state'];
// 	$assesss = pdo_fetchall("SELECT a.*,b.username,c.title FROM " . tablename('wff_assess'). " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id "."  WHERE a.uniacid = ".$_W['uniacid']." AND a.state=".$_GPC['state']);
// }else{
// 	$assesss = pdo_fetchall("SELECT a.*,b.username,c.title FROM " . tablename('wff_assess'). " a"  . " left join " . tablename("wff_register") . " b on a.u_id=b.id " . " left join ". tablename("wff_note") . " c on a.p_id=c.id "."  WHERE a.uniacid = ".$_W['uniacid']);
// }
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op=='delete'){
	$res=pdo_delete('wff_assess',array('id'=>$_GPC['id']));
	// var_dump($res);die;
	if($res){
	  message('删除成功',$this->createWebUrl('assess',array()),'success');
	}else{
	  message('删除失败','','error');
	}
}
if($_GPC['op']=='tg'){
  include_once IA_ROOT . '/addons/zh_wff/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_wff/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_wff/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('wff_assess',array('id'=>$_GPC['id']));  
  // if ($refund_order['pay']==2) {
      
        function getaccess_token($_W){
          $res=pdo_get('wff_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msg($_W,$_GPC){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('wff_system',array('uniacid'=>$_W['uniacid']));
            $openid=pdo_get('wff_assess',array('id'=>$_GPC['id']));
            $noteid=pdo_get('wff_note',array('id'=>$openid['p_id']));
            $user=pdo_get('wff_register',array('id'=>$openid['u_id']));
            $formwork ='{
                "touser": "'.$user["openid"].'",
                "template_id": "'.$ress["templeid"].'",
                "form_id":"'.$openid['formid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$noteid['title'].'  (评论审核通过)",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"审核结果",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        echo set_msg($_W,$_GPC);
        $res=pdo_update('wff_assess',array('state'=>2),array('id'=>$_GPC['id']));
        if($res){
          message('', 'referer', 'success');
        }else{
              message('失败！','','error');
        }
  // }
}
if($_GPC['op']=='jj'){
  include_once IA_ROOT . '/addons/zh_wff/cert/WxPay.Api.php';
  load()->model('account');
  load()->func('communication');
  $WxPayApi = new WxPayApi();
  $input = new WxPayRefund();
  $path_cert = IA_ROOT . "/addons/zh_wff/cert/".'apiclient_cert_' . $_W['uniacid'] . '.pem';
  $path_key = IA_ROOT . "/addons/zh_wff/cert/".'apiclient_key_' . $_W['uniacid'] . '.pem';
  $refund_order =pdo_get('wff_assess',array('id'=>$_GPC['id'])); 
	function getaccess_token($_W){
          $res=pdo_get('wff_system',array('uniacid'=>$_W['uniacid']));
          $appid=$res['appid'];
          $secret=$res['appsecret'];
          $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_URL,$url);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
          $data = curl_exec($ch);
          curl_close($ch);
          $data = json_decode($data,true);
          return $data['access_token'];
        }
        function set_msg($_W,$_GPC){
            $access_token = getaccess_token($_W);
            $ress=pdo_get('wff_system',array('uniacid'=>$_W['uniacid']));
            $openid=pdo_get('wff_assess',array('id'=>$_GPC['id']));
            $noteid=pdo_get('wff_note',array('id'=>$openid['p_id']));
            $user=pdo_get('wff_register',array('id'=>$openid['u_id']));
            $formwork ='{
                "touser": "'.$user["openid"].'",
                "template_id": "'.$ress["templeid"].'",
                "form_id":"'.$openid['formid'].'",
                "data": {
                    "keyword1": {
                        "value": "'.$noteid['title'].'  (评论审核未通过)",
                        "color": "#173177"
                    },
                    "keyword2": {
                        "value":"审核结果",
                        "color": "#173177"
                    }
                }  
            }';
            $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            curl_setopt($ch, CURLOPT_POST,1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
            $data = curl_exec($ch);
            curl_close($ch);
           return $data;
        }
        echo set_msg($_W,$_GPC);
        $res=pdo_update('wff_assess',array('state'=>3),array('id'=>$_GPC['id']));
        if($res){
          message('', 'referer', 'success');
        }else{
              message('失败！','','error');
        }
}
include $this->template('web/priassess');